package adventure.commands.skills;

import adventure.Adventure;
import adventure.AdventureEdit;
import adventure.placeable.Item;
import adventure.placeable.ItemImp;
import colour.Colour;
import colour.ColourUtil;
import player.Player;
import player.PlayerEdit;

/**
 *
 * @author Michael Hanns
 *
 */
public class ConjureItem extends AbstractSkill {

    private Item item;
    private boolean onFloor;
    protected final static String type = "CONJUREITEM";
    protected final static Colour typeColour = ColourUtil.darkCyan;

    public ConjureItem(Adventure a, String[] inputSynonyms, int id, Item i) {
        super(a, inputSynonyms, id);

        onFloor = false;

        item = i;
        usage = inputSynonyms[0];
        for (int x = 1; x < inputSynonyms.length; x++) {
            usage += ", " + inputSynonyms[x];
        }
    }

    public ConjureItem(Adventure a, int id) {
        super(a, new String[]{""}, id);

        onFloor = false;

        item = new ItemImp();
    }

    @Override
    public void setSynonyms(String[] syns) {
        this.synonyms = syns;

        usage = synonyms[0];
        for (int x = 1; x < synonyms.length; x++) {
            usage += ", " + synonyms[x];
        }
    }

    public void setItem(Item i) {
        this.item = i;
    }

    public void setOnFloor(boolean val) {
        this.onFloor = val;
    }

    @Override
    public String getType() {
        return type;
    }

    @Override
    public Colour getTypeColour() {
        return typeColour;
    }

    public int getItemID() {
        return item.getID();
    }

    public boolean onFloor() {
        return onFloor;
    }

    @Override
    protected String performSkill(Player player, String[] args) {
        if (args.length == 1) {
            boolean[] success = skillSuccess(player);
            
            if (success[0]) {
                if (onFloor) {
                    ((AdventureEdit) adv).getRoomManager().addItemToRoom(item, player.x(), player.y(), player.z());
                } else {
                    ((PlayerEdit) player).giveItem(item.cloneThis());
                }
            } else {
                consumePrerequisites(player);
                return ColourUtil.colourise(
                    synonyms[0].toUpperCase().substring(0,1)
                    + synonyms[0].toLowerCase().substring(1), typeColour) + " failed!";
            }
            consumePrerequisites(player);
            return generateConjureOutput(player);
        } else {
            return "Command not recognised.";
        }
    }

    private String generateConjureOutput(Player player) {
        String prefix = "";
        String prefixObserver = "";

        if (actionFlag == YOU) {
            prefix = ColourUtil.colourise("You", typeColour);
            prefixObserver = ColourUtil.colourise(player.getAliveName(), ColourUtil.player);
        } else if (actionFlag == YOUR) {
            prefix = ColourUtil.colourise("Your", typeColour);
            prefixObserver = ColourUtil.colourise(player.getAliveName(), ColourUtil.player);
        }

        String actionDescription = replaceActionKeywords(player);
        String actionObserver = replaceActionObserverKeywords(player);

        String itemName = ColourUtil.colourise(item.getName(), ColourUtil.item);

        out.broadcastLocalToPlayer(player,
                prefixObserver + " " + actionObserver + " "
                + itemName + ColourUtil.colourise(".", typeColour));

        return prefix + " " + actionDescription + " "
                + itemName + ColourUtil.colourise(".", typeColour);
    }

    private String replaceActionKeywords(Player player) {
        String[] words = actionDesc.split(" ");

        for (int x = 0; x < words.length; x++) {
            if (words[x].equals("[YOU]")) {
                words[x] = ColourUtil.colourise("YOU", typeColour);
            } else if (words[x].equals("[YOUR]")) {
                words[x] = ColourUtil.colourise("YOUR", typeColour);
            } else if (words[x].equals("[you]")) {
                words[x] = ColourUtil.colourise("you", typeColour);
            } else if (words[x].equals("[your]")) {
                words[x] = ColourUtil.colourise("your", typeColour);
            } else if (words[x].equals("[You]")) {
                words[x] = ColourUtil.colourise("You", typeColour);
            } else if (words[x].equals("[Your]")) {
                words[x] = ColourUtil.colourise("Your", typeColour);
            } else if (words[x].equals("[PLAYER]")) {
                words[x] = ColourUtil.colourise(player.getAliveName(), ColourUtil.player);
            } else if (words[x].equals("[PLAYERS]")) {
                words[x] = ColourUtil.colourise(player.getAliveName() + "'s", ColourUtil.player);
            } else {
                words[x] = ColourUtil.colourise(words[x], typeColour);
            }
        }

        if (words.length > 0) {
            String out = words[0];

            for (int x = 1; x < words.length; x++) {
                out += " " + words[x];
            }

            return out;
        }

        return actionDesc;
    }

    private String replaceActionObserverKeywords(Player player) {
        String[] words = actionDesc.split(" ");

        for (int x = 0; x < words.length; x++) {
            if (words[x].equals("[YOU]")) {
                words[x] = ColourUtil.colourise(player.getAliveName().toUpperCase(), ColourUtil.player);
            } else if (words[x].equals("[YOUR]")) {
                words[x] = ColourUtil.colourise(player.getAliveName().toUpperCase() + "'s", ColourUtil.player);
            } else if (words[x].equals("[you]")) {
                words[x] = ColourUtil.colourise(player.getAliveName(), ColourUtil.player);
            } else if (words[x].equals("[your]")) {
                words[x] = ColourUtil.colourise(player.getAliveName() + "'s", ColourUtil.player);
            } else if (words[x].equals("[You]")) {
                words[x] = ColourUtil.colourise(player.getAliveName(), ColourUtil.player);
            } else if (words[x].equals("[Your]")) {
                words[x] = ColourUtil.colourise(player.getAliveName() + "'s", ColourUtil.player);
            } else if (words[x].equals("[PLAYER]")) {
                words[x] = ColourUtil.colourise(player.getAliveName(), ColourUtil.player);
            } else if (words[x].equals("[PLAYERS]")) {
                words[x] = ColourUtil.colourise(player.getAliveName() + "'s", ColourUtil.player);
            } else {
                words[x] = ColourUtil.colourise(words[x], typeColour);
            }
        }

        if (words.length > 0) {
            String out = words[0];

            for (int x = 1; x < words.length; x++) {
                out += " " + words[x];
            }

            return out;
        }

        return actionDesc;
    }

    @Override
    public String getSkillHelp() {
        return synonyms[0] + "\n---\n"
                + "Learned at level " + levelLearned
                + "\nUsage(s): " + usage
                + "\nCreates : " + ColourUtil.colourise(item.getName(), ColourUtil.cyan) + " : "
                + item.getItemType() + ", Value: " + item.getModifier()
                + "\n" + description;
    }
}
